package com.wingice.system.validator.sys.role;

import com.wingice.system.constant.SecurityConstant;
import com.wingice.system.entity.SysRole;
import com.wingice.system.exception.SysBusinessCode;
import com.wingice.system.exception.SysBusinessException;
import com.wingice.system.validator.BaseValidator;
import org.springframework.util.StringUtils;

public class SysRoleValidator extends BaseValidator {

    public static void insertValidate(SysRole sysRole) {
        if (!StringUtils.hasText(sysRole.getName())) {
            throw new SysBusinessException(SysBusinessCode.ROLE_NAME_EMPTY_ERROR);
        }
        if (!StringUtils.hasText(sysRole.getRoleKey())) {
            throw new SysBusinessException(SysBusinessCode.ROLE_KEY_EMPTY_ERROR);
        }
        if (sysRole.getRoleType() == null) {
            throw new SysBusinessException(SysBusinessCode.ROLE_TYPE_NOT_EXIST_ERROR);
        }
        if (SecurityConstant.KNIFE_SUPER_ADMIN.equals(sysRole.getRoleKey())) {
            throw new SysBusinessException(SysBusinessCode.ILLEGAL_ROLE_KEY);
        }
    }

    public static void updateValidate(SysRole sysRole) {
        if (!StringUtils.hasText(sysRole.getId())) {
            throw new SysBusinessException(SysBusinessCode.ID_EMPTY_ERROR);
        }
        insertValidate(sysRole);
    }
}
